<?php
//include_once(PS_ADMIN_DIR.'/../classes/ObjectModel.php');
//include(dirname(__FILE__)."/PhotoClass.php");
class Zalbum extends ObjectModel 
{
	public $id;
	public $id_zgallery_album;
	
	public $id_parent;
	public $image;
	public $level_depth;
	
	public $nb_view=0;
	public $nb_like=0;
	public $limit_like=0;
	public $designer;
	public $location;
	public $model;
	public $style;
	public $photographer;
	public $priority_view;
	public $position;
	public $date_add;
	public $active=1;
	public $author;
	
	public $name;
	public $description;
	public $freurl;
	
	public $table = 'zgallery_album';
	protected $identifier = 'id_zgallery_album';
	
	protected $fieldsRequiredLang = array('name');
	protected $fieldsRequired =array('image');
	protected $fieldsSizeLang = array('name' => 50,'description'=>1000);
	protected $fieldsValidateLang = array('name' => 'isCatalogName','description'=>'isString');
	protected $url;
	protected $allow;
	
	public function getFields ()
	{
		parent::validateFields ();
		$fields['id_zgallery_album']=(int)($this->id);
		$fields['id_parent']=(int)($this->id_parent);
		$fields['image']=($this->image);
		$fields['level_depth']=(int)($this->level_depth);
		$fields['nb_view']=($this->nb_view);
		$fields['nb_like']=($this->nb_like);
		$fields['limit_like']=($this->limit_like);
		$fields['designer']=($this->designer);
		$fields['location']=($this->location);
		$fields['model']=($this->model);
		$fields['style']=($this->style);
		$fields['photographer']=($this->photographer);
		$fields['priority_view']=(int)($this->priority_view);
		$fields['author']=($this->author);
		
		$fields['date_add']=($this->date_add);
		$fields['active']=(int)($this->active);
		$fields['position']=(int)($this->position);
		
		return $fields;
	}
	
	public function __construct($id=NULL,$id_lang=null){
		$this->allow = (int)Configuration::get('PS_REWRITING_SETTINGS');
		$this->url = $_SERVER['SCRIPT_NAME'];
		parent::__construct ($id,$id_lang);
	}
	
	public function getTranslationsFieldsChild()
	{
		parent::validateFieldsLang();
		return parent::getTranslationsFields(array('name','description'));
	}
	
	static public function printNewsIconActive($id_pl_option, $tr)
	{
		$OptionClass = new Zalbum ($tr['id_zgallery_album']);
		echo 
		'<a href="index.php?tab=ZadminGallery&id_zgallery_album='.(int)($OptionClass->id).'&changeActive&token='.Tools::getAdminTokenLite('ZadminGallery').'">
			'.($OptionClass->active ? '<img src="../img/admin/enabled.gif" />' : '<img src="../img/admin/disabled.gif" />').'
		</a>
		';		 
	}
	
	public static function getAlbum($id_lang = false, $active = true, $order = true, $sql_filter = '', $sql_sort = '',$sql_limit = '')
	{
		
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
			SELECT *
			FROM `'._DB_PREFIX_.'zgallery_album` c
			LEFT JOIN `'._DB_PREFIX_.'zgallery_album_lang` cl ON c.`id_zgallery_album` = cl.`id_zgallery_album`
			WHERE 1 '.$sql_filter.' '.($id_lang ? 'AND `id_lang` = '.(int)($id_lang) : '').'
			'.($active ? 'AND `active` = 1' : '').'
			'.(!$id_lang ? 'GROUP BY c.id_zgallery_album' : '').'
			'.($sql_sort != '' ? $sql_sort : 'ORDER BY c.`level_depth` ASC, c.`position` ASC').'
			'.($sql_limit != '' ? $sql_limit : '')
		);
		
		if (!$order)
			return $result;

		$Albums = array();
		foreach ($result AS $row)
			$Albums[$row['id_parent']][$row['id_zgallery_album']]['infos'] = $row;
		return $Albums;
	}
	
	public static function recurseAlbum($Album, $current, $id_category = 1, $id_selected = 1)
	{
		echo '<option value="'.$id_category.'"'.(($id_selected == $id_category) ? ' selected="selected"' : '').'>'.
		str_repeat('&nbsp;', $current['infos']['level_depth'] * 5).stripslashes($current['infos']['name']).'</option>';
		if (isset($Album[$id_category]))
			foreach (array_keys($Album[$id_category]) AS $key)
				self::recurseAlbum($Album, $Album[$id_category][$key], $key, $id_selected);
	}
	
	public static function recurseAlbumFrontend($Album, $current, $id_category = 1, $id_selected = 1)
	{
		$rs= '<option value="'.$id_category.'"'.(($id_selected == $id_category) ? ' selected="selected"' : '').'>'.
		str_repeat('&nbsp;', $current['infos']['level_depth'] * 5).stripslashes($current['infos']['name']).'</option>';
		if (isset($Album[$id_category]))
			foreach (array_keys($Album[$id_category]) AS $key)
				$rs.= self::recurseAlbumFrontend($Album, $Album[$id_category][$key], $key, $id_selected);
		return $rs;
	}
	
	static public function getTotalPhoto ($id_album){
		$query = '
			SELECT COUNT(`id_zgallery_album`) as count FROM `'._DB_PREFIX_.'zgallery_photo` 
				WHERE `id_zgallery_album`='.$id_album.' AND active=1
				GROUP BY `id_zgallery_album` 
				
		';
		$results = Db::getInstance()->getRow($query);
		return $results['count'];
	}
	/**
	 * Get list album for id_parent
	 * @param unknown_type $id_parent
	 * @param unknown_type $p
	 * @param unknown_type $n
	 * @param unknown_type $id_lang
	 * @param unknown_type $active
	 * @param unknown_type $sql_sort
	 * @param unknown_type $sql_limit
	 */
	static public function getListAlbum ($id_parent, $p=1, $n=3,$id_lang, $active = true,  $sql_sort = '',$sql_limit = '') {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT * FROM '._DB_PREFIX_.'zgallery_album a
					LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang b ON (a.id_zgallery_album=b.id_zgallery_album )
					WHERE 
						a.id_parent='.$id_parent.' AND b.`id_lang` = '.(int)($id_lang).'
						'.($active ? 'AND `active` = 1' : '').'
						'.($sql_sort != '' ? $sql_sort : 'ORDER BY a.`position` ASC').'
						'.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : '') 
		);
		foreach ($result as $key => $value){
			$result[$key]['linkImage'] = __PS_BASE_URI__.'modules/zgallery/img/ab/'.$value['image'];
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$result[$key]['link'] = Zalbum::getLinkUrl($value['id_zgallery_album']);
			}
			else 
				$result[$key]['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-album.php?i='.$value['id_zgallery_album'];
			$result[$key]['totalPhoto'] = Zalbum::getTotalPhoto($value['id_zgallery_album']);
			$result[$key]['listItem'] = Zalbum::getListPhoto($value['id_zgallery_album'],$id_lang);
		}
		return $result;
	}
	/**
	 * using for padtion
	 * @param unknown_type $url
	 * @param unknown_type $p
	 */
	static public function goPage($url, $p)
	{
		return $url.($p == 1 ? '' : (!strstr($url, '?') ? '?' : '&amp;').'p='.(int)($p));
	}
	
	static public function goLink($url, $p)
	{
		return $url.($p == 1 ? '' : (!strstr($url, '?') ? '?' : '&amp;').'mode='.(int)($p));
	}
	
	static public function getPageLink($filename, $ssl = false, $id_lang = NULL)
	{
		global $cookie,$cache;
		if ($id_lang == NULL)
			$id_lang = (int)($cookie->id_lang);

		if ($this->allow == 1)
		{
			$url_rewrite = '';
			if ($filename != 'index.php')
			{
				$pagename = substr($filename, 0, -4);
				$url_rewrite = Db::getInstance()->getValue('
				SELECT url_rewrite
				FROM `'._DB_PREFIX_.'meta` m
				LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON (m.id_meta = ml.id_meta)
				WHERE id_lang = '.(int)($id_lang).' AND `page` = \''.pSQL($pagename).'\'');
				$uri_path = $this->getLangLink((int)$id_lang).($url_rewrite ? $url_rewrite : $filename);
			}
			else
				$uri_path = $this->getLangLink((int)$id_lang);
		}
		else
		{
			$uri_path = '';
			if ($filename != 'index.php')
				$uri_path = $filename;
		}
	
		return (($ssl AND Configuration::get('PS_SSL_ENABLED')) ? Tools::getShopDomainSsl(true) : Tools::getShopDomain(true)).__PS_BASE_URI__.ltrim($uri_path, '/');
	}
	
	public  function getLinkUrl ($i=NULL) {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		if ($i==NULL)
			if ( isset($_GET['i'])==0 ) {
				$i=1;
			}
			else $i=$_GET['i'];
		$frl = Zalbum::getParents ($i);
		$frl=$frl['freurl'];
		$out = $linkHome."album-gallery";
		$out.='/'.$i.'_'."$frl";
		return $out;
	} 
	
	
	static public function goPageThumb($url, $n)
	{
		return $url.(!strstr($url, '?') ? '?' : '&amp;').'n='.(int)($n);
	}
	
	public function getPaginationLinkPageNThumb($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
	{
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		if ($type AND $id_object)
			$url = $this->{'get'.$type.'Link'}($id_object, NULL);
		else
		{
			$url=$this->url;
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$url = $this->getLinkUrl ();
			}
		}
		$vars = (!$array ? '' : array());
		$varsNb = array('n', 'search_query');
		$varsSort = array('orderby', 'orderway');
		$varsPagination = array('p','n');
		
		$n = 0;
		foreach ($_GET AS $k => $value)
			if ($k != 'id_'.$type)
			{
				if (Configuration::get('PS_REWRITING_SETTINGS') AND ($k == 'isolang' OR $k == 'id_lang' OR $k=='i' OR $k == '_php?isolang'))
					continue;
				$ifNb = (!$nb OR ($nb AND !in_array($k, $varsNb)));
				$ifSort = (!$sort OR ($sort AND !in_array($k, $varsSort)));
				$ifPagination = (!$pagination OR ($pagination AND !in_array($k, $varsPagination)));
				if ($ifNb AND $ifSort AND $ifPagination AND !is_array($value))
					!$array ? ($vars .= ((!$n++ AND ($this->allow == 1 OR $url == $this->url)) ? '?' : '&').urlencode($k).'='.urlencode($value)) : ($vars[urlencode($k)] = urlencode($value));
			}
		if (!$array)
			return $url.$vars;
		$vars['requestUrl'] = $url;
		if ($type AND $id_object)
			$vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
		return $vars;
	}
	
	
	
	public function getPaginationLink($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
	{
		global $cookie,$link;
		$album = new Zalbum ();
		$linkHome = $link->getPageLink('');
		if ($type AND $id_object)
			$url = $this->{'get'.$type.'Link'}($id_object, NULL);
		else
		{
			$url=$this->url;			
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$url = $this->getLinkUrl ();
			}
		}
		$vars = (!$array ? '' : array());
		$varsNb = array('n', 'search_query');
		$varsSort = array('orderby', 'orderway');
		$varsPagination = array('p');
		
		$n = 0;
		foreach ($_GET AS $k => $value)
			if ($k != 'id_'.$type)
			{
				if (Configuration::get('PS_REWRITING_SETTINGS') AND ($k == 'isolang' OR $k == 'id_lang' OR $k=='i' OR $k == '_php?isolang'))
					continue;
				$ifNb = (!$nb OR ($nb AND !in_array($k, $varsNb)));
				$ifSort = (!$sort OR ($sort AND !in_array($k, $varsSort)));
				$ifPagination = (!$pagination OR ($pagination AND !in_array($k, $varsPagination)));
				if ($ifNb AND $ifSort AND $ifPagination AND !is_array($value))
					!$array ? ($vars .= ((!$n++ AND ($this->allow == 1 OR $url == $this->url)) ? '?' : '&').urlencode($k).'='.urlencode($value)) : ($vars[urlencode($k)] = urlencode($value));
			}
		if (!$array)
			return $url.$vars;
		$vars['requestUrl'] = $url;
		if ($type AND $id_object)
			$vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
		return $vars;
	}
	
	static public function getLink($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
	{
		global $cookie,$link;
		$linkHome = $link->getPageLink('');	
		if ($type AND $id_object)
			$url = $this->{'get'.$type.'Link'}($id_object, NULL);
		else
		{
			$url = $this->url;
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$url = $this->getLinkUrl ();
			}
		}
		$vars = (!$array ? '' : array());
		$varsNb = array('mode', 'search_query');
		$varsSort = array('orderby', 'orderway');
		$varsPagination = array('mode');

		$n = 0;
		foreach ($_GET AS $k => $value)
			if ($k != 'id_'.$type)
			{
				if (Configuration::get('PS_REWRITING_SETTINGS') AND ($k == 'isolang' OR $k == 'id_lang' OR $k=='i'))
					continue;
				$ifNb = (!$nb OR ($nb AND !in_array($k, $varsNb)));
				$ifSort = (!$sort OR ($sort AND !in_array($k, $varsSort)));
				$ifPagination = (!$pagination OR ($pagination AND !in_array($k, $varsPagination)));
				if ($ifNb AND $ifSort AND $ifPagination AND !is_array($value))
					!$array ? ($vars .= ((!$n++ AND ($this->allow == 1 OR $url == $this->url)) ? '?' : '&').urlencode($k).'='.urlencode($value)) : ($vars[urlencode($k)] = urlencode($value));
			}
		if (!$array)
			return $url.$vars;
		$vars['requestUrl'] = $url;
		if ($type AND $id_object)
			$vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
		return $vars;
	}
	
	static public function getNumberAlbums ($id_item,$id_ct){
		$t=time();
		if ($id_ct==2)
			$t-=604800;
		else if ($id_ct==1)
			$t-=30*86400;
		$time = date("Y-m-d H:i:s",$t);
		
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT COUNT(`id_zgallery_album`) AS "nbr"
			FROM `'._DB_PREFIX_.'zgallery_album`
			WHERE position'.($id_item==0?'!=-1':'='.$id_item).' '.($id_ct!=0?'AND date_add>"'.$time.'"':'').' AND active = 1 AND priority_view=0
			');
		return (int)($result['nbr']);
	}
	
	
	static public function getListPhoto ($id_zgallery_album,$id_lang, $active = true,  $sql_sort = '',$sql_limit = '') {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT * FROM '._DB_PREFIX_.'zgallery_photo a
					LEFT JOIN '._DB_PREFIX_.'zgallery_photo_lang b ON (a.id_zgallery_photo=b.id_zgallery_photo )
					WHERE 
						a.id_zgallery_album='.$id_zgallery_album.' AND b.`id_lang` = '.(int)($id_lang).'
						'.($active ? 'AND `active` = 1' : '').'
						'.($sql_sort != '' ? $sql_sort : 'ORDER BY a.`date_add` ASC').'
						'.($sql_limit != '' ? $sql_limit : '') 
		);
		foreach ($result as $key => $value){
			$result[$key]['linkImage'] = __PS_BASE_URI__.'modules/zgallery/img/photo/'.$value['image'];
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$result[$key]['link'] = $linkHome.'photo-gallerypro-a?i='.$value['id_zgallery_album'].'&v='.$value['id_zgallery_photo'];
			}
			else 
				$result[$key]['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgalleryPhoto.php?i='.$value['id_zgallery_album'].'&v='.$value['id_zgallery_photo'];
		}
		return $result;
	}
	
	
	/**
	 * 
	 * get level depth with id_parent
	 * @param int $id_parent
	 * @return int level depth id_parent +1
	 */
	static public function getLevelDepth ($id_parent) {
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT `level_depth` AS "nbr"
			FROM `'._DB_PREFIX_.'zgallery_album`
			WHERE `id_zgallery_album` = '.$id_parent);
		return (int)($result['nbr']+1);
	}

	static public function getNameImage ($id) {
		if ($id!=NULL) {
			$query = '
				SELECT image FROM `'._DB_PREFIX_.'zgallery_album` 
					WHERE `id_zgallery_album`='.$id.'
			';
			$results = Db::getInstance()->getRow($query);
			return $results['image'];
		}
		else return '';
	}
	
	static public function getParents ($id) {
		global $cookie;
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT * FROM '._DB_PREFIX_.'zgallery_album a
			LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang b ON (a.id_zgallery_album=b.id_zgallery_album AND b.id_lang='.$cookie->id_lang.')
			WHERE a.id_zgallery_album='.$id.' 
		');
//		if (Configuration::get('PS_REWRITING_SETTINGS')) {
//			$result['linkPage'] = Zalbum::getLinkUrl($result['id_zgallery_album']);
//		}
//		else 
//			$result['linkPage'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-album.php?i='.$result['id_zgallery_album'];
		return $result;
	}
	/**
  	* Return path to a album category
  	*
  	* @param string $urlBase Start URL
  	* @param integer $id_category Start category
  	* @param string $path Current path
  	* @param string $highlight String to highlight (in XHTML/CSS)
  	* @param string $type Category type (products/cms)
  	*/
	static function getPath($id, $path = '', $token = '')
	{
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
	
		$rs=array();
		$parent = Zalbum::getParents($id);
		if (Configuration::get('PS_REWRITING_SETTINGS'))
			$parent['link'] = Zalbum::getLinkUrl($parent['id_zgallery_album']); 
		else
			$parent['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-album.php?id_zgallery_album='.$parent['id_zgallery_album'];
		$parent['adminLink'] = '<a href="'.$path.'&id_zgallery_album='.(int)$parent['id_zgallery_album'].'&addzgallery_album&token='.$token.'" title="'.($parent['id_zgallery_album'] == 1 ? 'Home' : 'Modify').'"><img src="../img/admin/'.($parent['id_zgallery_album'] == 1 ? 'home' : 'edit').'.gif" alt="" /></a>
								<a href="'.$path.'&id_zgallery_album='.(int)$parent['id_zgallery_album'].'&viewzgallery_album&token='.$token.'" title="'.($parent['id_zgallery_album'] == 1 ? 'Home' : 'Modify').'">&nbsp;'.$parent['name'].'</a>
		';
		array_push($rs, $parent);
		while ($parent['id_parent']>0) {
			$parent = Zalbum::getParents($parent['id_parent']);
			if (Configuration::get('PS_REWRITING_SETTINGS'))
				$parent['link'] = Zalbum::getLinkUrl($value['id_zgallery_album']);
			else
				$parent['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-album.php?i='.$parent['id_zgallery_album'];
			$parent['adminLink'] = '<a href="'.$path.'&id_zgallery_album='.(int)$parent['id_zgallery_album'].'&addzgallery_album&token='.$token.'" title="'.($parent['id_zgallery_album'] == 1 ? 'Home' : 'Modify').'"><img src="../img/admin/'.($parent['id_zgallery_album'] == 1 ? 'home' : 'edit').'.gif" alt="" /></a>
									<a href="'.$path.'&id_zgallery_album='.(int)$parent['id_zgallery_album'].'&viewzgallery_album&token='.$token.'" title="'.($parent['id_zgallery_album'] == 1 ? 'Home' : 'Modify').'">&nbsp;'.$parent['name'].'</a>
			';
			array_push($rs, $parent);
		}
		$rs2 = array();
		for ($i=sizeof($rs)-1 ; $i>=0;$i--)
			array_push($rs2, $rs[$i]);
		return $rs2;
	}
	
	static function getIdParrent ($id) {
		global $cookie;
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT `id_parent` FROM '._DB_PREFIX_.'zgallery_album a
			LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang b ON (a.id_zgallery_album=b.id_zgallery_album AND b.id_lang='.$cookie->id_lang.')
			WHERE a.id_zgallery_album='.$id.' 
		');
		return $result['id_parent'];
	}
	
	static function getParentCurrent ($id) {
		global $cookie,$link;
		$linkHome = $link->getPageLink('');
		$rs=array();
		
		if ($id==1) {
			$rs['linkPage'] =  $linkHome;
			$rs['name'] =  'Home';
		}
		else {
			$id_parent = Zalbum::getIdParrent($id);
			$rs = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
				SELECT * FROM '._DB_PREFIX_.'zgallery_album a
				LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang b ON (a.id_zgallery_album=b.id_zgallery_album AND b.id_lang='.$cookie->id_lang.')
				WHERE a.id_zgallery_album='.$id_parent.' 
			');
			if (Configuration::get('PS_REWRITING_SETTINGS'))
				$rs['linkPage'] = Zalbum::getLinkUrl($rs['id_zgallery_album']);
			else
				$rs['linkPage'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-album.php?i='.$rs['id_zgallery_album'];
		}
		return $rs;
	}
	
	static public function getImageName  ($id) {
		$rs = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT * FROM '._DB_PREFIX_.'zgallery_album a
			WHERE a.id_zgallery_album='.$id.' 
		');
		return $rs['image'];
	}
	
	/**
	 * Delete several objects from database
	 *
	 * return boolean Deletion result
	 */
	public function deleteSelection($selection)
	{
		if (!is_array($selection) OR !Validate::isTableOrIdentifier($this->identifier) OR !Validate::isTableOrIdentifier($this->table))
			die(Tools::displayError());
		$result = true;
		foreach ($selection AS $id)
		{
			$this->id = (int)($id);
			$name = Zalbum::getImageName ($id);
			Zalbum::_deleteImage($id,$name);
			$result = $result AND $this->delete();
		}
		return $result;
	}
	
	static public function _deleteImage($id,$name)
	{
		$target = dirname(__FILE__).'/../img/ab/'.$name;
		if (file_exists($target) AND !unlink($target))
			return false;
		return true;
	}
	
	static public function getValueTitle () {
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(
			'SELECT * FROM '._DB_PREFIX_.'zgallery_album_title'
		);
		return $result;
	}
	/**
	 * Get All Album for items // Trang Phuc Phu Kien ...
	 * if $id_item == 0 get all
	 * id_ct: 
	 * 		0: all
	 *  	1: trong vong 1 thang 
	 *  	2: trong vong  7 ngay  
	 */
	
	static public function getAlbumsItem ($id_item,$id_ct,$id_lang,$p,$n=7) {
		$t=time();
		if ($id_ct==2)
			$t-=604800;
		else if ($id_ct==1)
			$t-=30*86400;
		$time = date("Y-m-d H:i:s",$t);
		
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
			SELECT * FROM '._DB_PREFIX_.'zgallery_album a
			LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang b ON (a.id_zgallery_album=b.id_zgallery_album AND b.id_lang='.$id_lang.')
			WHERE a.position'.($id_item==0?'!=-1':'='.$id_item).' '.($id_ct!=0?'AND a.date_add>"'.$time.'"':'').' AND active = 1 AND priority_view=0
			ORDER BY a.nb_like DESC
			'.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : '').'
		');
		foreach ($result as $key => $value){
			$result[$key]['linkImage'] = __PS_BASE_URI__.'modules/zgallery/img/ab/'.$value['image'];
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$result[$key]['link'] = Zalbum::getLinkUrl($value['id_zgallery_album']);
			}
			else 
				$result[$key]['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-album.php?i='.$value['id_zgallery_album'];
			$result[$key]['totalPhoto'] = Zalbum::getTotalPhoto($value['id_zgallery_album']);			
		}
		return $result;
	}
	/**
	 * Get Album for id 
	 * use get info album  
	 */
	static public function getAlbumCurrent ($id) {
		$photo = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT * FROM '._DB_PREFIX_.'zgallery_photo 
					WHERE id_zgallery_photo='.$id.' 
		');
		$id_album = $photo['id_zgallery_album'];
		if (Configuration::get('PS_REWRITING_SETTINGS')) {
			$result = Zalbum::getLinkUrl($value['id_zgallery_album']);
		}
		else 
			$result = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-album.php?i='.$id_album.'&v='.$id;
				
		return $result;
	}
	/**
	 * Get all album set priotity 
	 */
	static public function getAlbumPV ($id_item,$id_lang) {
		$result =  Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
			SELECT * FROM '._DB_PREFIX_.'zgallery_album a
			LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang b ON (a.id_zgallery_album=b.id_zgallery_album AND b.id_lang='.$id_lang.') 
			WHERE  priority_view=1 AND a.position='.$id_item.'
		');
		foreach ($result as $key => $value){
			$result[$key]['linkImage'] = __PS_BASE_URI__.'modules/zgallery/img/ab/'.$value['image'];
			if (Configuration::get('PS_REWRITING_SETTINGS')) {
				$result[$key]['link'] = Zalbum::getLinkUrl($value['id_zgallery_album']);
			}
			else 
				$result[$key]['link'] = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zgallery/zgallery-album.php?i='.$value['id_zgallery_album'];
			$result[$key]['totalPhoto'] = Zalbum::getTotalPhoto($value['id_zgallery_album']);			
		}
		return $result;
	}
	/**
	 * get name album for id_photo
	 */
	static public function getNameAlbum ($id_photo,$id_lang) {
		$result =  Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT `name` FROM '._DB_PREFIX_.'zgallery_photo p
			LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang al ON (p.id_zgallery_album=al.id_zgallery_album)
			WHERE al.id_lang='.$id_lang.' AND p.id_zgallery_photo='.$id_photo.'
		');
		return $result['name'];
	}
	
	static public function getInfosCurrent ($id_item) {
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT `name` FROM '._DB_PREFIX_.'zgallery_album_title a
			WHERE position='.$id_item.'
		');
		return $result['name'];
	}
	static public function getNameAlbumCurrent ($id_album) {
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT al.name FROM '._DB_PREFIX_.'zgallery_album a
			LEFT JOIN '._DB_PREFIX_.'zgallery_album_lang al ON (a.id_zgallery_album=al.id_zgallery_album)
			WHERE a.id_zgallery_album='.$id_album.'
		');
		return $result['name'];
	}
	/**
	 * get number photo in album
	 */
	static public function getNumberPhoto ($id_album) {
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
			SELECT COUNT(`id_zgallery_photo`) AS "nbr"
			FROM `'._DB_PREFIX_.'zgallery_photo`
			WHERE active = 1 AND id_zgallery_album='.$id_album.'
			');
		return (int)($result['nbr']);
	}
}

